package com.wm.blog_admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_admin.controller.base.AdminBaseController;
import com.wm.blog_admin.query.TIconQuery;
import com.wm.blog_admin.service.TIconService;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.context.AdminBaseContextHandler;
import com.wm.blog_common.domain.TIconDO;
import com.wm.blog_common.entity.TIcon;
import com.wm.blog_common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 半卷流年
 * @since 2020-04-15
 */
@Slf4j
@RestController
@RequestMapping("/blog/icon")
public class TIconController extends AdminBaseController {

    @Autowired
    private TIconService tIconService;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<TIconDO> get(@PathVariable Long id) {
        return tIconService.selectById(id);
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id){
        return tIconService.deleteById(id);
    }

    /**
     * 添加
     * @param tIcon
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody TIcon tIcon){
        return tIconService.insert(tIcon);
    }

    /**
     * 修改
     * @param tIcon
     * @return
     */
    @PutMapping
    public Result update(@RequestBody TIcon tIcon){
        return tIconService.update(tIcon);
    }

    /**
     * 分页查询
     * @param current   查询页
     * @param size   每页显示条数
     * @param query 查询参数
     * @return
     */
    @GetMapping("/page")
    public Result<Page<TIconDO>> page(Integer current, Integer size, TIconQuery query){
        return tIconService.page(new QueryPage(current,size),query);
    }

    @PostMapping("/uploadExcel")
    public Result uploadExcel(MultipartFile file){
        return tIconService.uploadExcel(file);
    }

    /**
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteIcon")
    public Result deleteIcon(Long id){
        return tIconService.deleteIcon(id);
    }


    /**
     *下载icon批量上传模板
     * @return
     */
    @GetMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response){
        tIconService.downTemplate(response);
    }



    /**
     *下载icon批量上传模板
     * @return
     */
    @GetMapping("/iconList")
    public Result iconList(){
        return tIconService.iconList();
    }

    /**
     *详情
     * @return
     */
    @GetMapping("/getById")
    public Result getById(Long id){
        return tIconService.getById(id);
    }



    /**
     *详情
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody TIconQuery query){
        query = Optional.ofNullable(query).orElse(new TIconQuery());
        return tIconService.saveOrUpdate(query);
    }


    /**
     *详情
     * @return
     */
    @PutMapping("/topIcon")
    public Result topIcon(Long id,HttpServletRequest request){
        Long adminId = AdminBaseContextHandler.getAdminUserId();
        TIconQuery query = new TIconQuery();
        query.setAdminId(adminId);
        query.setId(id);
        return tIconService.topIcon(query);
    }


    /**
     *excel下载
     * @return
     */
    @GetMapping("/exportIcon")
    public void export(TIconQuery query,HttpServletResponse response){
        query = Optional.ofNullable(query).orElse(new TIconQuery());
        tIconService.export(query,response);
    }

}
